package sub

import (
	"fmt"
	"quipus/cmd/qpc/sub"
	"quipus/internal/utils"
	v1pb "quipus/proto/gen/api/v1"
	apb "quipus/proto/gen/assist"
	"strings"

	"github.com/spf13/cobra"
)

var (
	storageType string
)

func init() {
	storageCmd.PersistentFlags().StringVarP(&storageType, "type", "t", "LOCAL", "storage type:local,s3,minio,database")
	systemCmd.AddCommand(storageCmd)
}

var storageCmd = &cobra.Command{
	Use:   "storage",
	Short: "Run the cmd to set storage service",
	RunE: func(cmd *cobra.Command, args []string) error {
		qconn, e := sub.NewGrpcConnect(sub.QpConfig.QPAddress)
		if e != nil {
			return e
		}

		qclient := v1pb.NewSystemSettingServiceClient(qconn)
		ctxWithToken := sub.NewCtxWithToken(sub.QpConfig.Token)
		urn := utils.BuildSystemKey(utils.SystemSettingUrnPrefix, apb.SystemSettingKey_STORAGE.String())
		ss := strings.ToUpper(storageType)
		value := &v1pb.SystemStorageSetting{
			StorageType:       v1pb.StorageType(v1pb.StorageType_value[ss]),
			UploadSizeLimitMb: 100}
		setting := &v1pb.SystemSetting{Urn: urn, Value: &v1pb.SystemSetting_StorageSetting{
			StorageSetting: value,
		}}
		fmt.Println(value)
		in := &v1pb.SetSystemSettingRequest{Setting: setting}

		rsp, err := qclient.SetSystemSetting(ctxWithToken, in)
		if err != nil {
			return err
		}
		fmt.Print(rsp)
		return nil
	},
}
